package com.qi.demo01.JMM.Volatile;

/**
 * @author zjq
 * @date 2020/7/6 20:59
 * <p>
 * 验证volatile的可见性
 */
public class Demo01 {
    //保证可见性
    public static void main(String[] args) {
        MyData myData = new MyData();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "come in");
            myData.addTo60();
            System.out.println("number的值" + myData.numble);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        //第二个线程是我们的main线程
        while (myData.numble == 0) {
            System.out.println("number的值为" + myData.numble);
        }
        System.out.println("更改后的number的值为" + myData.numble);

    }
}

class MyData {
    volatile int numble = 0;

    public void addTo60() {
        this.numble = 60;
    }
}